﻿using AnyLink.Domain;
using AnyLink.Infrastructure.Services;
using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Mvc;

namespace AnyLink.WebApi.Controllers.Common
{
    [ApiController]
    public class CaptchaController : BaseController
    {
        private readonly ICaptcha _captcha;
        private readonly SysConfigService _sysConfigService;
        public CaptchaController(ICaptcha captcha, SysConfigService sysConfigService)
        {
            _captcha = captcha;
            _sysConfigService = sysConfigService;
        }

        /// <summary>
        /// 生成图片验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet("captchaImage")]
        public IActionResult CaptchaImage()
        {
            ApiResult ajax = ApiResult.Success();

            bool captchaEnabled = _sysConfigService.SelectCaptchaEnabled();
            ajax.Put("captchaEnabled", captchaEnabled);
            if (!captchaEnabled)
            {
                return ToResponse(ajax);
            }
            string uuid = Guid.NewGuid().ToString().Replace("-", "");

            var info = _captcha.Generate(uuid, 60);
            ajax.Put("uuid", uuid);
            ajax.Put("img", info.Base64);
            return ToResponse(ajax);
        }
    }
}
